import {getInfo, userInfo} from '@/api/user/index'

import {login} from '@/api/login/index'
import {getToken, setToken, removeToken} from '@/utils/auth'

// 动态路由
function buildRouter(routeMap, menus, zh, en) {
  menus.filter(menu => {
    if (menu.component) {
      routeMap.push({
        name: menu.name,
        path: menu.path,
        component: () => import('@/views/' + menu.component)
      });
    }
    zh[menu.code] = menu.name;
    en[menu.code] = menu.localeEn;
  });
}

// 动态菜单
function buildMenu(menus, m, b) {
  let tmpMap = {};
  let i, l;
  for (i = 0, l = menus.length; i < l; i++) {
    tmpMap[menus[i].code] = menus[i];
  }
  for (i = 0, l = menus.length; i < l; i++) {
    // 菜单
    if (menus[i].code.length === 3) {
      m.push(menus[i]);
    } else {
      if (!tmpMap[menus[i].code.substring(0,menus[i].code.length - 3)]['children']) {
        tmpMap[menus[i].code.substring(0,menus[i].code.length - 3)]['children'] = [];
      }
      tmpMap[menus[i].code.substring(0,menus[i].code.length - 3)]['children'].push(menus[i]);
    }

    // 面包屑
    if (menus[i].path && menus[i].path !== '') {
      let m,arr = [],len = menus[i].code.length / 3;
      for (m = 1; m < len + 1; m++) {
        arr.push(tmpMap[menus[i].code.substring(0, 3 * m)]);
      }
      b[menus[i].path] = arr;
    }
  }
}

const user = {
  state: {
    menus: undefined,
    routes: undefined,
    breadcrumb: [],
    token: getToken(),
    userInfo: undefined,
    localeZh: undefined,
    localeEn: undefined
  },
  mutations: {
    SET_MENUS: (state, menus) => {
      state.menus = menus;
    },
    SET_ROUTES: (state, routes) => {
      state.routes = routes;
    },
    SET_BREADCRUMB: (state, breadcrumb) => {
      state.breadcrumb = breadcrumb;
    },
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_USER_INFO: (state, userInfo) => {
      state.userInfo = userInfo;
    },
    SET_LOCALE_ZH: (state, localeZh) => {
      state.localeZh = localeZh;
    },
    SET_LOCALE_EN: (state, localeEn) => {
      state.localeEn = localeEn;
    },
  },
  actions: {

    login({commit}, userInfo) {
      commit('SET_TOKEN', '');
      commit('SET_MENUS', undefined);
      commit('SET_ROUTES', undefined);
      removeToken();
      return new Promise((resolve, reject) => {
        //login 执行api中fetch方法

        login(userInfo.userName.trim(), userInfo.password).then(response => {
          setToken(response.data);
          commit('SET_TOKEN', response.data);// setToken(response.token);
          resolve();
        }).catch(error => {
          reject(error);
        });
      })
    },
    loginOut({commit}) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '');
        commit('SET_MENUS', undefined);
        commit('SET_ROUTES', undefined);
        commit('SET_USER_INFO', undefined);
        removeToken();
        resolve();
      });
    },
    findInfo({commit, state}) {
      return new Promise((resolve, reject) => {
        getInfo(state.token).then(data => {
          commit('SET_USER_INFO', data.userInfo);
          let m = [];
          let b = {};
          buildMenu(data.menus, m, b);
          commit('SET_MENUS', m);
          commit('SET_BREADCRUMB', b);
          let r = [];
          let zh = {};
          let en = {};
          buildRouter(r, data.menus, zh, en);
          commit('SET_LOCALE_ZH', zh);
          commit('SET_LOCALE_EN', en);
          commit('SET_ROUTES', r);
          resolve();
        }).catch(error => {
          reject(error);
        })
      })
    }
  }
};
export default user;
